﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Mengsk.ProxyDelivery.Service.Delivery
{

    class ZJSCodComputer : IDeliveryMoneyComputer
    {
        static List<DeliveryArea> Areas = new List<DeliveryArea>();

        static DeliveryArea defaultArea = new DeliveryArea { Province = "", Start = 1, StartPrice = 10, Step = 1, StepPrice = 6 };

        static ZJSCodComputer()
        {
            Areas.Add(new DeliveryArea { Province = "安徽", Start = 1.0, StartPrice = 10, Step = 1, StepPrice = 6 });
            Areas.Add(new DeliveryArea { Province = "北京", Start = 1.0, StartPrice = 10, Step = 1, StepPrice = 8 });
            Areas.Add(new DeliveryArea { Province = "重庆", Start = 1, StartPrice = 8, Step = 1, StepPrice = 3 });
            Areas.Add(new DeliveryArea { Province = "福建", Start = 1.0, StartPrice = 12, Step = 1, StepPrice = 8 });
            Areas.Add(new DeliveryArea { Province = "甘肃", Start = 1.0, StartPrice = 12, Step = 1, StepPrice = 8 });
            Areas.Add(new DeliveryArea { Province = "广东", Start = 1.0, StartPrice = 12, Step = 1, StepPrice = 9 });
            Areas.Add(new DeliveryArea { Province = "广西", Start = 1.0, StartPrice = 12, Step = 1, StepPrice = 9 });
            Areas.Add(new DeliveryArea { Province = "贵州", Start = 1.0, StartPrice = 10, Step = 1, StepPrice = 5 });
            Areas.Add(new DeliveryArea { Province = "海南", Start = 1.0, StartPrice = 10, Step = 1, StepPrice = 10 });
            Areas.Add(new DeliveryArea { Province = "河北", Start = 1.0, StartPrice = 12, Step = 1, StepPrice = 8 });
            Areas.Add(new DeliveryArea { Province = "河南", Start = 1.0, StartPrice = 10, Step = 1, StepPrice = 8 });
            Areas.Add(new DeliveryArea { Province = "黑龙江", Start = 1.0, StartPrice = 13, Step = 1, StepPrice = 8 });
            Areas.Add(new DeliveryArea { Province = "湖北", Start = 1.0, StartPrice = 10, Step = 1, StepPrice = 6 });
            Areas.Add(new DeliveryArea { Province = "湖南", Start = 1.0, StartPrice = 10, Step = 1, StepPrice = 6 });
            Areas.Add(new DeliveryArea { Province = "吉林", Start = 1.0, StartPrice = 14, Step = 1, StepPrice = 9 });
            Areas.Add(new DeliveryArea { Province = "江苏", Start = 1.0, StartPrice = 10, Step = 1, StepPrice = 8 });
            Areas.Add(new DeliveryArea { Province = "江西", Start = 1.0, StartPrice = 12, Step = 1, StepPrice = 8 });
            Areas.Add(new DeliveryArea { Province = "辽宁", Start = 1.0, StartPrice = 11, Step = 1, StepPrice = 9 });
            Areas.Add(new DeliveryArea { Province = "内蒙古", Start = 1.0, StartPrice = 13, Step = 1, StepPrice = 10 });
            Areas.Add(new DeliveryArea { Province = "宁夏", Start = 1.0, StartPrice = 12, Step = 1, StepPrice = 6 });
            Areas.Add(new DeliveryArea { Province = "青海", Start = 1.0, StartPrice = 12, Step = 1, StepPrice = 6 });
            Areas.Add(new DeliveryArea { Province = "山东", Start = 1.0, StartPrice = 12, Step = 1, StepPrice = 6 });
            Areas.Add(new DeliveryArea { Province = "山西", Start = 1.0, StartPrice = 11, Step = 1, StepPrice = 8 });
            Areas.Add(new DeliveryArea { Province = "陕西", Start = 1.0, StartPrice = 11, Step = 1, StepPrice = 6 });
            Areas.Add(new DeliveryArea { Province = "上海", Start = 1.0, StartPrice = 10, Step = 1, StepPrice = 6 });
            Areas.Add(new DeliveryArea { Province = "四川", Start = 1.0, StartPrice = 11, Step = 1, StepPrice = 2 });
            Areas.Add(new DeliveryArea { Province = "天津", Start = 1.0, StartPrice = 10, Step = 1, StepPrice = 6 });
            Areas.Add(new DeliveryArea { Province = "西藏", Start = 1.0, StartPrice = 16, Step = 1, StepPrice = 12 });
            Areas.Add(new DeliveryArea { Province = "新疆", Start = 1.0, StartPrice = 20, Step = 1, StepPrice = 10 });
            Areas.Add(new DeliveryArea { Province = "云南", Start = 1.0, StartPrice = 13, Step = 1, StepPrice = 4 });
            Areas.Add(new DeliveryArea { Province = "浙江", Start = 1.0, StartPrice = 10, Step = 1, StepPrice = 6 });
        }


        public bool Accept(Domain.OrderPopPayType payType, string deliveryCompany)
        {
            return payType == Domain.OrderPopPayType.COD && deliveryCompany.Contains("宅急送");
        }

        public float ComputeMoney(Domain.OrderPopPayType payType, string deliveryCompany, double weight, string receiverAddress)
        {
            var pro = AddressService.ParseProvince(receiverAddress);
            if (pro == null)
            {
                throw new Exception("没有地址中的省:" + receiverAddress);
            }

            var area = Areas.FirstOrDefault(obj => pro.Name.Contains(obj.Province));
            if (area == null)
            {
                area = defaultArea;
            }
            int step = 1000;
            int stepCount = ((int)(weight * 1000) + step - 1 - 1000) / step;
            if (stepCount < 0)
            {
                stepCount = 0;
            }
            return (float)(Math.Round(area.StartPrice + area.StepPrice * stepCount));
        }
    }
}
